package com.atguigu.crowd.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.atguigu.crowd.entity.po.AddressPO;
import com.atguigu.crowd.entity.po.AddressPOExample;
import com.atguigu.crowd.entity.po.AddressPOExample.Criteria;
import com.atguigu.crowd.entity.po.OrderPO;
import com.atguigu.crowd.entity.po.OrderProjectPO;
import com.atguigu.crowd.entity.po.ProjectPO;
import com.atguigu.crowd.entity.vo.AddressVO;
import com.atguigu.crowd.entity.vo.DetailProjectVO;
import com.atguigu.crowd.entity.vo.OrderProjectVO;
import com.atguigu.crowd.entity.vo.OrderVO;
import com.atguigu.crowd.mapper.AddressPOMapper;
import com.atguigu.crowd.mapper.OrderPOMapper;
import com.atguigu.crowd.mapper.OrderProjectPOMapper;
import com.atguigu.crowd.mapper.ProjectPOMapper;
import com.atguigu.crowd.service.api.OrderService;

@Service
@Transactional(readOnly = true)
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderProjectPOMapper orderProjectPOMapper;
	
	@Autowired
	private OrderPOMapper orderPOMapper;
	
	@Autowired
	private AddressPOMapper addressPOMapper;

	@Autowired
	private ProjectPOMapper projectPOMapper;
	
	@Override
	public OrderProjectVO getOrderProjectVO(Integer returnId) {
		OrderProjectVO orderProjectVO = orderProjectPOMapper.selectOrderProjectVO(returnId);
		System.out.println(orderProjectVO);
		return orderProjectVO;
	}

	@Override
	public List<AddressVO> getAddressVOList(Integer memberId) {
		AddressPOExample addressPOExample = new AddressPOExample();
		addressPOExample.createCriteria().andMemberIdEqualTo(memberId);
		List<AddressPO> list = addressPOMapper.selectByExample(addressPOExample);
		
		//拷贝addressPO到addressVO，并且设置到addressVOList中
		AddressVO addressVO = new AddressVO();
		List<AddressVO> addressVOList = new ArrayList<AddressVO>();
		for (AddressPO addressPO : list) {
			BeanUtils.copyProperties(addressPO, addressVO);
			addressVOList.add(addressVO);
		}
		return addressVOList;
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	@Override
	public void saveAddressVO(AddressVO addressVO) {
		AddressPO addressPO = new AddressPO();
		BeanUtils.copyProperties(addressVO, addressPO);
		addressPOMapper.insert(addressPO);
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	@Override
	public void saveOrderVO(OrderVO orderVO,Integer memberId) {
		OrderPO orderPO = new OrderPO();
		BeanUtils.copyProperties(orderVO, orderPO);
		OrderProjectVO orderProjectVO = orderVO.getOrderProjectVO();
		orderPOMapper.insert(orderPO);
		
		OrderProjectPO orderProjectPO = new OrderProjectPO();
		BeanUtils.copyProperties(orderProjectVO, orderProjectPO);
		//获取订单项目的订单外键，避免外键约束导致插入数据失败
		//需要在mapper.xml中设置useGeneratedKeys="true" keyProperty="id"
		Integer orderId = orderPO.getId();
		orderProjectPO.setOrderId(orderId);
		orderProjectPO.setMemberId(memberId);
		orderProjectPOMapper.insert(orderProjectPO);
		//获取项目id，需要在mapper.xml中设置useGeneratedKeys="true" keyProperty="id"
		Integer projectId = orderProjectPO.getProjectId();
		//查询该项目
		ProjectPO projectPO = projectPOMapper.selectByPrimaryKey(projectId);
		//获取项目对象，并把支持的金额设置到项目对象中并更新项目筹集金额，
		//若筹集金额大于等于目标金额则设置众筹状态为众筹成功
		Long oldsupportmoney = projectPO.getSupportmoney();
		Long money = projectPO.getMoney();
		//获取订单金额
		Long orderMoney = new Double(orderPO.getOrderAmount()).longValue();
		Long newsupportmoney = oldsupportmoney+orderMoney;
		//设置支持金额
		projectPO.setSupportmoney(newsupportmoney);
		if (money<=newsupportmoney) {//支持金额大于等于目标金额，设置众筹状态为众筹成功
			projectPO.setStatus(3);
		}
		//获取支持人数
		Integer supporter = projectPO.getSupporter();
		projectPO.setSupporter(supporter+1);
		//更新项目
		projectPOMapper.updateByPrimaryKeySelective(projectPO);
	}

	@Override
	public List<OrderProjectVO> getOrderProject(Integer memberId) {
		List<OrderProjectVO> orderProjectList = orderProjectPOMapper.selectOrderProjectList(memberId);
		for (OrderProjectVO orderProject : orderProjectList) {
			
			//设置剩余天数
			String deployDate = orderProject.getDeployDate();
			Date currentDate = new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date deployDay = format.parse(deployDate);
				
				// 获取当前当前日期的时间戳
				long currentTimeStamp = currentDate.getTime();
				
				// 获取众筹日期的时间戳
				long deployTimeStamp = deployDay.getTime();
				
				// 两个时间戳相减计算当前已经过去的时间
				long pastDays = (currentTimeStamp - deployTimeStamp) / 1000 / 60 / 60 / 24;
				
				// 获取总的众筹天数
				Integer totalDays = orderProject.getDay();
				
				// 使用总的众筹天数减去已经过去的天数得到剩余天数
				Integer lastDay = (int) (totalDays - pastDays);
				if (lastDay<=0) {//若剩余天数<=0则剩余天数为0且众筹状态设为已关闭
					lastDay = 0;
					Integer money = orderProject.getMoney();
					Integer supportMoney = orderProject.getSupportMoney();
					if(money>supportMoney) {//若筹集天数结束，筹集金额小于目标金额，则设置众筹状态为众筹失败
						orderProject.setStatusText("已关闭");
					}else {
						orderProject.setStatusText("众筹成功");
					}
				}
				orderProject.setLastDay(lastDay);
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
			//设置众筹状态
			Integer status = orderProject.getStatus();
			switch(status) {
				case 0:
					orderProject.setStatusText("审核中");
					break;
				case 1:
					orderProject.setStatusText("众筹中");
					break;
				case 2:
					orderProject.setStatusText("众筹成功");
					break;
				case 3:
					orderProject.setStatusText("已关闭");
					break;
				default:
					break;
			}
		}
		
		return orderProjectList;
	}

	@Override
	public OrderProjectVO showOrderDetail(Integer orderId) {
		OrderProjectVO orderDetail =  orderPOMapper.selectOrderDetail(orderId);
	
			
			//设置剩余天数
			String deployDate = orderDetail.getDeployDate();
			Date currentDate = new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			try {
				Date deployDay = format.parse(deployDate);
				
				// 获取当前当前日期的时间戳
				long currentTimeStamp = currentDate.getTime();
				
				// 获取众筹日期的时间戳
				long deployTimeStamp = deployDay.getTime();
				
				// 两个时间戳相减计算当前已经过去的时间
				long pastDays = (currentTimeStamp - deployTimeStamp) / 1000 / 60 / 60 / 24;
				
				// 获取总的众筹天数
				Integer totalDays = orderDetail.getDay();
				
				// 使用总的众筹天数减去已经过去的天数得到剩余天数
				Integer lastDay = (int) (totalDays - pastDays);
				if (lastDay<=0) {//若剩余天数<=0则剩余天数为0且众筹状态设为已关闭
					lastDay = 0;
					Integer money = orderDetail.getMoney();
					Integer supportMoney = orderDetail.getSupportMoney();
					if(money>supportMoney) {//若筹集天数结束，筹集金额小于目标金额，则设置众筹状态为众筹失败
						orderDetail.setStatusText("已关闭");
					}else {
						orderDetail.setStatusText("众筹成功");
					}
				}
				orderDetail.setLastDay(lastDay);
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
			Integer status = orderDetail.getStatus();
			switch(status) {
				case 0:
					orderDetail.setStatusText("审核中");
					break;
				case 1:
					orderDetail.setStatusText("众筹中");
					break;
				case 2:
					orderDetail.setStatusText("众筹成功");
					break;
				case 3:
					orderDetail.setStatusText("已关闭");
					break;
				default:
					break;
			}
		
		return orderDetail;
		
	}

	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW,rollbackFor=Exception.class)
	@Override
	public void delOrderDetail(Integer orderId) {
		
		orderPOMapper.deleteByPrimaryKey(orderId);
		orderProjectPOMapper.deleteOrderProject(orderId);
	
	}

	

	

	

}
